<?php
$extra = '';
if (isset($_POST['chat_box']) && !empty($_POST['chat_box']) && $_POST['chat_box'] == 'Siųsti' && !empty($_POST['chat_msg']) && isset($_SESSION['username']) && !empty($_SESSION['username'])) {
	$msg = htmlspecialchars($_POST['chat_msg']); $nick = $_SESSION['username']; $nick_id = $_SESSION['id'];
	mysql_query1("INSERT INTO `".LENTELES_PRIESAGA."chat_box` (`nikas`, `msg`, `time`, `niko_id`) VALUES (".escape($nick).", ".escape($msg).", NOW(), ".escape($nick_id).");");
}
function chatbox() {
	global $conf; $extra = '';
	if (isset($_SESSION['username']) && !empty($_SESSION['username'])) {
		$chat_box = '<form name="chat_box" action="" method="post">
		<textarea name="chat_msg" rows="3" cols="10" style="width:95%"></textarea>
		<br />
		<input type="submit" name="chat_box" value="Siųsti" />
		</form>
		';
	}
	else {
		$chat_box = '<textarea name="chat" rows="3" cols="10" style="width:95%" disabled="disabled">Turite prisijungti.</textarea>
		<br />
		<input type="submit" name="chat_box" value="Siųsti" disabled="disabled" />
	';
	}
	$chat_box .= "<hr />"; $chat = mysql_query1("SELECT * FROM `".LENTELES_PRIESAGA."chat_box` ORDER BY `time` DESC LIMIT ".$conf['Chat_limit']."");
	while($row = mysql_fetch_assoc($chat)) {
		if (isset($_SESSION['username']) && $row['nikas'] != $_SESSION['username']) { 
		$extra = "
			<a href=\"?id,45;n,1;u,".$row['nikas']."\"><img src=\"images/pm/mail.png\" alt=\"pm\" border=\"0\" /></a>
		"; 
		}
		
		if (defined("LEVEL") && LEVEL >= 30) { 
		$extra .= "
			<a href='?id,49;d,".$row['id']."'><img src='images/icons/bullet_delete.png' alt='[d]' class='middle' border='0' /></a>
			<a href='?id,49;r,".$row['id']."'><img src='images/icons/bullet_orange.png' alt='[r]' class='middle' border='0' /></a>
			<a href='?id,49;b,".$row['niko_id']."'><img src='images/icons/bullet_red.png' alt='[b]' class='middle' border='0' /></a>
		"; }
		$chat_box .= '
		<div class="sarasas">
		<a href="?id,47;m,'.$row['niko_id'].'" title="header=['.$row['nikas'].'] body=[<br/>Data: <b>'.$row['time'].'</b><br/>Kada: <b>'.kada($row['time']).'</b><br/><br/>] fade=[on]"><b>'.trimlink($row['nikas'],8).'</b></a>
		'.$extra.'<br />
			'.wrap(smile(bbchat($row['msg'])),13).'
		</div>';
		$extra='';
	}
	if (isset($_SESSION['username'])) { $chat_box .= "<a href='?id,49' title='Archyvas'>Archyvas</a>"; }
	return $chat_box;	
}

$text = chatbox();
?>
